package com.project.class3_29;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class XMLPress {
    public List<StudentBean> press(){
        List<StudentBean> list = new ArrayList<>();
        SAXReader sax= new SAXReader();
        try {
            //读取文档内容，加载至Document文档对象中
            Document doc=sax.read("src/com/project/xml/lovo.xml");
            //选择指定节点进行解析,选择lovoClass/student 标记进行解析。每个student标记就是一个Element元素
            List<Element> elementList=doc.selectNodes("/lovoClass/student");
            for (Element element : elementList){
                StudentBean student= new StudentBean();
                //将student标记的code属性值取出，封装student的code属性
                student.setCode(element.attributeValue("code"));
                student.setPhone(element.attributeValue("phone"));

                //将student标记的子标记name的标记内容取出，封装student对象的name属性
                student.setName(element.elementText("name"));
                student.setEdu(element.elementText("edu"));
                student.setAge(Integer.parseInt(element.elementText("age")));

                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(new XMLPress().press());
    }
}
